package cn.edu.cdp.gui.frame;

import java.awt.*;

import cn.edu.cdp.dao.CourseDao;
import cn.edu.cdp.dao.StudentDao;
import cn.edu.cdp.domain.Account;
import cn.edu.cdp.domain.Course;
import cn.edu.cdp.domain.Student;
import cn.edu.cdp.gui.panel.AdminPanel;
import cn.edu.cdp.gui.panel.StudentPanel;
import cn.edu.cdp.gui.panel.TeacherPanel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import javax.swing.*;

@Component
public class MainFrame extends JFrame {
    private final CourseDao courseDao;
    private final StudentDao studentDao;
    private final AdminPanel adminPanel;
    private final StudentPanel studentPanel;
    private final TeacherPanel teacherPanel;

    @Autowired
    public MainFrame(CourseDao courseDao, StudentDao studentDao, AdminPanel adminPanel, StudentPanel studentPanel, TeacherPanel teacherPanel) {
        initComponents();
        this.courseDao = courseDao;
        this.studentDao = studentDao;
        this.adminPanel = adminPanel;
        this.studentPanel = studentPanel;
        this.teacherPanel = teacherPanel;
    }

    public void decoration(Account account) {
        this.setLocation(100, 100);
        switch (account.getRole()) {
            case "管理员" -> {
                adminPanel.decoration();
                JScrollPane scrollPane = new JScrollPane(adminPanel);
                this.setTitle("管理员：admin");
                this.add(scrollPane);
            }
            case "教师" -> {
                Course course = courseDao.getCourseById(account.getUsername());
                teacherPanel.decoration(course);
                this.setTitle("课程（教师）：" + course.getName());
                this.add(teacherPanel);
            }
            case "学生" -> {
                Student student = studentDao.basicInfo(account.getUsername());
                studentPanel.decoration(student);
                this.setTitle("学生：" + student.getName());
                this.add(studentPanel, BorderLayout.CENTER);
            }
        }
        this.pack();
    }

    private void initComponents() {
        // JFormDesigner - Component initialization - DO NOT MODIFY  //GEN-BEGIN:initComponents  @formatter:off

        //======== this ========
        setDefaultCloseOperation(WindowConstants.EXIT_ON_CLOSE);
        setResizable(false);
        var contentPane = getContentPane();
        contentPane.setLayout(new BorderLayout());
        pack();
        // JFormDesigner - End of component initialization  //GEN-END:initComponents  @formatter:on
    }
}
